$(function(){
	
	var service = new EbeimService();
    var jform = $("#j-form"),
        jsubmit = $("#j-submit"),
        jphone = $("#j-phone");
    
    var phone = plugin.GetQueryString("mobile");
    jphone.val(phone.substring(0,3)+"****"+phone.substring(7,11));

    //占位符初始化
    plugin.placeHolder({
        con: $(".j-placeHolder")
    });

    //表单验证初始化
    jform.find(".j-formcheck").bind('focus',function() {
        var t = $(this);
        var error = t.siblings('.error');
        error.hide();
    });
    /*jform.find(".j-formcheck").bind("blur", function() {
        var t = $(this);
        var name = t.attr("data-name");
        formCheck(jform,name);
    }).bind('focus',function() {
        var t = $(this);
        var error = t.siblings('.error');
        error.hide();
    });*/

    //表单提交
    jform.bind("submit", function(){
        if (formCheck(jform)) {
            jsubmit.attr("disabled", "disabled");
            
            var data = jform.serialize();
            data = data + "&userId="+ plugin.GetQueryString("userId") +"&token=tokennawang"+"&type=1";
            
            data = plugin.formToJson(data);

            service.oncePasswordUpdate(data, function(json){
                if(json.code == 200) {
                	plugin.msgtip({type: "o",msg: json.desc});
                	setTimeout(function(){
                		window.location.href = "../html/login.html";
                    }, 1500);
                }else{
                	jsubmit.removeAttr("disabled");
                	plugin.msgtip({type: "i",msg: json.desc});
                }
            },function(){
            	jsubmit.removeAttr("disabled");
            });
        }
        return false;
    });

        //记录定时器
        var timer = "";
        var countDown = function(dom,time){
            dom.val(time+"s");
            timer = setInterval(function(){
                if(time>0){
                    time--;
                    dom.val(time+"s");
                }else{
                    clearInterval(timer);
                    dom.removeAttr("disabled").removeClass("disableBtn").addClass("codeBtn").val("获取短信验证码");
                }
            },1000);
        }
        //绑定短信发送事件
        jform.delegate(".j-getCode","click",function(){
            var data = {
                token:"tokennawang",
                username: plugin.GetQueryString("username")

            }
            var t = $(this);
            t.attr("disabled", "disabled").addClass("disableBtn");
            countDown(t,60);
            service.getMessageCode(data,function(json){
                if(json.code == 200) {
                	plugin.msgtip({type: "o",msg: json.desc});
                }else{
                	plugin.msgtip({type: "i",msg: json.desc});
                }
            })
        });

})

// 表单验证
var formCheck = function(form,name) {

	// 显示错误信息
    var showError = function(obj, msg) {
        var error = obj.siblings('.error');
        msg = msg + "<i></i>"
        error.html(msg);
        error.show();
    };

    // 隐藏错误信息
    var hideError = function(obj) {
        var error = obj.siblings('.error');
        error.hide();
    };

    // 数据验证对象
    var dataCheck = {
        code: {
            obj: form.find("[data-name='code']"),
            fn: function() {
            	this.obj.val(this.obj.val().replace(/(^\s+)|(\s+$)/g,""));
                var val = this.obj.val();
                if (plugin.checkFn.isNull(val)) {
                    showError(this.obj,"请输入验证码");
                    return false;
                } else {
                    hideError(this.obj);
                    return true;
                }
            }
        },
        newPassword: {
            obj: form.find("[data-name='newPassword']"),
            fn: function() {
                var val = this.obj.val();
                if (plugin.checkFn.isNull(val)) {
                    showError(this.obj,"请输入新密码");
                    return false;
                } else if(!plugin.checkFn.isPwd(val)){
                	showError(this.obj,"至少六位的英文数字组合");
                    return false;
                }else if(plugin.checkFn.isHowLong(val,50)){
                	showError(this.obj,"新密码过长");
                    return false;
                } else {
                    hideError(this.obj);
                    return true;
                }
            }
        },
        passwordSecond: {
            obj: form.find("[data-name='passwordSecond']"),
            fn: function() {
                var val = this.obj.val();
                if (plugin.checkFn.isNull(val)) {
                    showError(this.obj,"请再次输入新密码");
                    return false;
                } else if(val != dataCheck["newPassword"].obj.val()){
                    showError(this.obj,"两次输入的密码不一致");
                    return false;
                } else {
                    hideError(this.obj);
                    return true;
                }
            }
        }
    };

    if (name && dataCheck[name]) {
        return dataCheck[name].fn();
    } else {
        var errLen = 0;
        for (var i in dataCheck) {
            if (!dataCheck[i].fn()) {
                errLen++;
            }
        }
        if (errLen > 0) {
            return false;
        }
        return true;
    }
}
